]>
Commit | Line | Data |
---|---|---|
1 | using System; | |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | using System.Text; | |
5 | using Microsoft.Xna.Framework; | |
6 | using Microsoft.Xna.Framework.Content; | |
7 | using Microsoft.Xna.Framework.Graphics; | |
8 | ||
9 | namespace SuperPolarity | |
10 | { | |
11 | class Actor | |
12 | { | |
13 | protected SuperPolarity game; | |
14 | ||
15 | public List<Actor> Children; | |
16 | ||
17 | // Graphics / In-Game | |
18 | protected Texture2D Texture; | |
19 | protected Vector2 Origin; | |
20 | public bool Active; | |
21 | public Rectangle Box; | |
22 | public Vector4 BoxDimensions; | |
23 | protected Texture2D BoxTexture; | |
24 | ||
25 | // Physical Properties | |
26 | public Vector2 Position; | |
27 | protected Vector2 Velocity; | |
28 | protected Vector2 Acceleration; | |
29 | public float Angle; | |
30 | ||
31 | // Constraints / Behavior | |
32 | public float MaxVelocity; | |
33 | protected float AccelerationRate; | |
34 | public int HP; | |
35 | protected bool Immortal; | |
36 | public bool Dying; | |
37 | public int Value; | |
38 | protected Color Color; | |
39 | ||
40 | public Actor Parent; | |
41 | ||
42 | public int Width | |
43 | { | |
44 | get { return Texture.Width; } | |
45 | } | |
46 | ||
47 | public int Height | |
48 | { | |
49 | get { return Texture.Height; } | |
50 | } | |
51 | ||
52 | public Actor(SuperPolarity newGame) | |
53 | { | |
54 | game = newGame; | |
55 | BoxDimensions.X = 20; | |
56 | BoxDimensions.Y = 20; | |
57 | BoxDimensions.W = 15; | |
58 | BoxDimensions.Z = 15; | |
59 | } | |
60 | ||
61 | public virtual void Initialize(Texture2D texture, Vector2 position) | |
62 | { | |
63 | Texture = texture; | |
64 | Position = position; | |
65 | Active = true; | |
66 | ||
67 | Children = new List<Actor>(); | |
68 | ||
69 | Origin = new Vector2(Texture.Width / 2, Texture.Height / 2); | |
70 | Velocity = new Vector2(0, 0); | |
71 | Acceleration = new Vector2(0, 0); | |
72 | ||
73 | MaxVelocity = 5; | |
74 | AccelerationRate = 10; | |
75 | ||
76 | HP = 1; | |
77 | Immortal = false; | |
78 | ||
79 | Dying = false; | |
80 | Value = 1; | |
81 | ||
82 | InitBox(); | |
83 | BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1); | |
84 | BoxTexture.SetData(new Color[] { Color.White }); | |
85 | ||
86 | Color = Color.White; | |
87 | } | |
88 | ||
89 | protected void InitBox() | |
90 | { | |
91 | Box = new Rectangle((int)(Position.X - BoxDimensions.X), (int)(Position.Y - BoxDimensions.X), (int)(BoxDimensions.X + BoxDimensions.X + BoxDimensions.W), (int)(BoxDimensions.Y + BoxDimensions.Y + BoxDimensions.Z)); | |
92 | } | |
93 | ||
94 | public void AutoDeccelerate(GameTime gameTime) | |
95 | { | |
96 | if (Acceleration.X == 0 && Velocity.X > 0) | |
97 | { | |
98 | if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X) | |
99 | { | |
100 | Velocity.X = 0; | |
101 | Acceleration.X = 0; | |
102 | } | |
103 | else | |
104 | { | |
105 | Acceleration.X = -AccelerationRate; | |
106 | } | |
107 | } | |
108 | ||
109 | if (Acceleration.X == 0 && Velocity.X < 0) | |
110 | { | |
111 | if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X) | |
112 | { | |
113 | Velocity.X = 0; | |
114 | Acceleration.X = 0; | |
115 | } | |
116 | else | |
117 | { | |
118 | Acceleration.X = AccelerationRate; | |
119 | } | |
120 | } | |
121 | ||
122 | if (Acceleration.Y == 0 && Velocity.Y > 0) | |
123 | { | |
124 | if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y) | |
125 | { | |
126 | Velocity.Y = 0; | |
127 | Acceleration.Y = 0; | |
128 | } | |
129 | else | |
130 | { | |
131 | Acceleration.Y = -AccelerationRate; | |
132 | } | |
133 | } | |
134 | ||
135 | if (Acceleration.Y == 0 && Velocity.Y < 0) | |
136 | { | |
137 | if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y) | |
138 | { | |
139 | Velocity.Y = 0; | |
140 | Acceleration.Y = 0; | |
141 | } | |
142 | else | |
143 | { | |
144 | Acceleration.Y = AccelerationRate; | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
149 | public virtual void Update(GameTime gameTime) | |
150 | { | |
151 | Move(gameTime); | |
152 | ChangeAngle(); | |
153 | CheckOutliers(); | |
154 | UpdateBox(); | |
155 | } | |
156 | ||
157 | protected virtual void UpdateBox() | |
158 | { | |
159 | Box.X = (int)(Position.X - BoxDimensions.X); | |
160 | Box.Y = (int)(Position.Y - BoxDimensions.Y); | |
161 | } | |
162 | ||
163 | public virtual void Move(GameTime gameTime) | |
164 | { | |
165 | AutoDeccelerate(gameTime); | |
166 | ||
167 | var maxVelocity = MaxVelocity; | |
168 | ||
169 | Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds; | |
170 | Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; | |
171 | ||
172 | if (Velocity.X > MaxVelocity) | |
173 | { | |
174 | Velocity.X = MaxVelocity; | |
175 | } | |
176 | ||
177 | if (Velocity.X < -MaxVelocity) | |
178 | { | |
179 | Velocity.X = -MaxVelocity; | |
180 | } | |
181 | ||
182 | if (Velocity.Y > MaxVelocity) | |
183 | { | |
184 | Velocity.Y = MaxVelocity; | |
185 | } | |
186 | ||
187 | if (Velocity.Y < -MaxVelocity) | |
188 | { | |
189 | Velocity.Y = -MaxVelocity; | |
190 | } | |
191 | ||
192 | Position.X = Position.X + Velocity.X; | |
193 | Position.Y = Position.Y + Velocity.Y; | |
194 | } | |
195 | ||
196 | public void ChangeAngle() | |
197 | { | |
198 | if (Math.Abs(Velocity.Y) <= 0.1 && Math.Abs(Velocity.X) <= 0.1) | |
199 | { | |
200 | return; | |
201 | } | |
202 | Angle = (float)Math.Atan2(Velocity.Y, Velocity.X); | |
203 | } | |
204 | ||
205 | public virtual void Draw(SpriteBatch spriteBatch) | |
206 | { | |
207 | foreach (Actor child in Children) | |
208 | { | |
209 | child.Draw(spriteBatch); | |
210 | } | |
211 | ||
212 | spriteBatch.Draw(Texture, Position, null, Color, Angle, Origin, 1f, SpriteEffects.None, 0f); | |
213 | spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25)); | |
214 | } | |
215 | ||
216 | void CheckOutliers() | |
217 | { | |
218 | for (var i = Children.Count; i > 0; i--) | |
219 | { | |
220 | var actor = Children[i - 1]; | |
221 | if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds || | |
222 | actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds || | |
223 | actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds) | |
224 | { | |
225 | Children.Remove(actor); | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
230 | public virtual void Collide(Actor other, Rectangle collision) | |
231 | { | |
232 | } | |
233 | ||
234 | public void TakeDamage(int amount) | |
235 | { | |
236 | if (!Immortal) | |
237 | { | |
238 | HP = HP - amount; | |
239 | if (HP < 0) | |
240 | { | |
241 | Die(); | |
242 | } | |
243 | } | |
244 | } | |
245 | ||
246 | protected virtual void Die() | |
247 | { | |
248 | Dying = true; | |
249 | } | |
250 | } | |
251 | } |